<?php
class Schedule_model extends Mymodel {
	public $table_name = "schedule";
    function Schedule_model()
    {
        parent::Mymodel();
    }
	
	function get_appointment($id=NULL, $condition=NULL)
	{
		return $this->get_data($this->table_name, $id, $condition,'timestamp','ASC');
	}	
		
	function insert_new_apointment($data)
	{
		if(!empty($data['date']))
			unset($data['date']);
			
		$result=$this->get_data($this->table_name,NULL,array('year'=>$data['year'],'month'=>$data['month'],'day'=>$data['day'],'time'=>$data['time'],'user_id'=>$data['user_id']));
		#$this->show_the_query();die;
        if($result['count']==0)
		return $this->insert_new_record($this->table_name , $data);
		if($result['count']>0)
		return false;
	}
	function update_apointment_by_id($data,$id){
		$time=substr($data['date'],-8);
		$d_array=explode('/',$data['date']);
		$day=$d_array[1];
		$month=$d_array[0];
		$year=substr($d_array[2],0,4);
	
		unset($data['date']);
		$result=$this->get_data($this->table_name,NULL,array('year'=>$year,'month'=>$month,'day'=>$day,'time'=>$time,'user_id'=>$data['user_id']));
		//$this->show_the_query();
		foreach ($result as $dt){
			if($dt['id']==$id) $result['count']--; 
		}
		#die($result['count'].'gggggggg');
        if($result['count']==0)
		return $this->update_by_id($this->table_name, $data, $id );
		return false;
		
	}
	
}
